package pt.mleiria.crypto.signatures;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Signature;

public class KeyPairGenMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("Usage: KeyPairGenMain nomeDoFicheiroParaAssinar");
		} else {
			final KeyPairGen kpg = new KeyPairGen();
			try {
				//Obter o objecto Signature
				final Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
				//Inicializar o objecto Signature
				dsa.initSign(kpg.getPriv());
				//Forncer o objecto Signature aos dados a serem assinados
//				final FileInputStream fis = new FileInputStream(args[0]);
				final FileInputStream fis = new FileInputStream
						("D:\\workspace\\Private_WS\\cryptoApp\\Crypto\\src\\main\\java\\pt\\mleiria\\crypto\\signatures\\manuelLeiria");
				final BufferedInputStream bufin = new BufferedInputStream(fis);
				final byte[] buffer = new byte[1024];
				int len;
				while ((len = bufin.read(buffer)) >= 0) {
					dsa.update(buffer, 0, len);
				}
				bufin.close();
				//Gerar a assinatura
				final byte[] realSig = dsa.sign();
				//Gravar a assinatura num ficheiro
				final FileOutputStream sigfos = new FileOutputStream("sig");
				sigfos.write(realSig);
				sigfos.close();
				//Gravar a chave pública num ficheiro
				final byte[] key = kpg.getPub().getEncoded();
				FileOutputStream keyfos = new FileOutputStream("mypk");
				keyfos.write(key);
				keyfos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
